package com.anyext.storage;

import java.util.List;
import java.util.Map;

public interface SqlMapTemplate {

	<V> Map<String, V> select(String statement) throws SessionException;

	<V> Map<String, V> select(String statement, Object... parameters) throws SessionException;

	<T> T selectOne(String statement) throws SessionException;

	/**
	 * Retrieve a single row mapped from the statement key and parameter.
	 * @param <T> the returned object type
	 * @param statement Unique identifier matching the statement to use.
	 * @param parameter A parameter object to pass to the statement.
	 * @return Mapped object
	 */
	<T> T selectOne(String statement, Object... parameters) throws SessionException;

	<E> List<E> selectList(String statement) throws SessionException;

	/**
	 * Retrieve a list of mapped objects from the statement key and parameter.
	 * @param <E> the returned list element type
	 * @param statement Unique identifier matching the statement to use.
	 * @param parameter A parameter object to pass to the statement.
	 * @return List of mapped object
	 */
	<E> List<E> selectList(String statement, Object... parameters) throws SessionException;

	<E> List<E> selectList(String statement, Page page) throws SessionException;

	/**
	 * Retrieve a list of mapped objects from the statement key and parameter,
	 * within the specified row bounds.
	 * @param <E> the returned list element type
	 * @param statement Unique identifier matching the statement to use.
	 * @param parameter A parameter object to pass to the statement.
	 * @param pageBounds to limit object retrieval
	 * @return List of mapped object
	 */
	<E> List<E> selectList(String statement, Page page, Object... parameters) throws SessionException;

	<K, V> Map<K, V> selectMap(String statement, String mapKey) throws SessionException;

	/**
	 * The selectMap is a special case in that it is designed to convert a list
	 * of results into a Map based on one of the properties in the resulting
	 * objects.
	 * Eg. Return a of Map[Integer,Author] for selectMap("selectAuthors","id")
	 * @param <K> the returned Map keys type
	 * @param <V> the returned Map values type
	 * @param statement Unique identifier matching the statement to use.
	 * @param mapKey The property to use as key for each value in the list.
	 * @return Map containing key pair data.
	 */
	<K, V> Map<K, V> selectMap(String statement, String mapKey, Object... parameters) throws SessionException;

	<K, V> Map<K, V> selectMap(String statement, String mapKey, Page page) throws SessionException;

	/**
	 * The selectMap is a special case in that it is designed to convert a list
	 * of results into a Map based on one of the properties in the resulting
	 * objects.
	 * @param <K> the returned Map keys type
	 * @param <V> the returned Map values type
	 * @param statement Unique identifier matching the statement to use.
	 * @param parameter A parameter object to pass to the statement.
	 * @param mapKey The property to use as key for each value in the list.
	 * @param pageBounds to limit object retrieval
	 * @return Map containing key pair data.
	 */
	<K, V> Map<K, V> selectMap(String statement, String mapKey, Page page, Object... parameters) throws SessionException;

	int insert(String statement) throws SessionException;

	/**
	 * Execute an insert statement with the given parameter object. Any generated
	 * autoincrement values or selectKey entries will modify the given parameter
	 * object properties. Only the number of rows affected will be returned.
	 * @param statement Unique identifier matching the statement to execute.
	 * @param parameter A parameter object to pass to the statement.
	 * @return int The number of rows affected by the insert.
	 */
	int insert(String statement, Object... parameters) throws SessionException;

	int update(String statement) throws SessionException;

	/**
	 * Execute an update statement. The number of rows affected will be returned.
	 * @param statement Unique identifier matching the statement to execute.
	 * @param parameter A parameter object to pass to the statement.
	 * @return int The number of rows affected by the update.
	 */
	int update(String statement, Object... parameters) throws SessionException;

	int delete(String statement) throws SessionException;

	/**
	 * Execute a delete statement. The number of rows affected will be returned.
	 * @param statement Unique identifier matching the statement to execute.
	 * @param parameter A parameter object to pass to the statement.
	 * @return int The number of rows affected by the delete.
	 */
	int delete(String statement, Object... parameters) throws SessionException;

}
